package cn.mastor.org.service.impl;

import cn.mastor.basic.config.BussinessException;
import cn.mastor.basic.query.BaseQuery;
import cn.mastor.basic.service.impl.BaseServiceImpl;
import cn.mastor.basic.util.BaiduAuditUtils;
import cn.mastor.basic.util.PageList;
import cn.mastor.org.domain.Employee;
import cn.mastor.org.domain.Shop;
import cn.mastor.org.domain.ShopAuditLog;
import cn.mastor.org.enu.ShopStateEum;
import cn.mastor.org.mapper.EmployyMapper;
import cn.mastor.org.mapper.ShopAuditLogMapper;
import cn.mastor.org.mapper.ShopMapper;
import cn.mastor.org.service.IShopService;
import cn.mastor.org.vo.ShopEchartsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-15
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopAuditLogMapper auditLogMapper;
    @Autowired
    private JavaMailSender sender;
    @Autowired
    private EmployyMapper employeeMapper;
    @Override
    public PageList PageList2(BaseQuery query){
        Integer totals = shopMapper.queryByCount(query);
        if (null == totals || 0 == totals){
            return new PageList(0,new ArrayList());
        }
//        auditLogMapper.findbyshopId()
        // 查询数据if (employees.get(i).getId()==)
        List<Shop> employees = shopMapper.queryByPage(query);
        for (int i = 0; i < employees.size(); i++) {
            List list = auditLogMapper.findbyshopId(employees.get(i).getId());
            employees.get(i).setLog(list);
            System.out.println(employees.get(i));
        }
//        System.out.println(employees);
        //方到返回里面
        return new PageList(totals,employees);
    }
    @Override
    public Map<String, Object> echarts() {
        Map<String,Object> res=new HashMap<>();
        List<ShopEchartsVo> vo =shopMapper.echarts();
        List<Integer> x = vo.stream().map(ShopEchartsVo::getState).collect(Collectors.toList());
        List<Integer> y = vo.stream().map(ShopEchartsVo::getCount).collect(Collectors.toList());
        List<String> realX = new ArrayList<>();
        for (Integer xTemp : x) {
            String stateName = ShopStateEum.getNameByCode(xTemp);
            realX.add(stateName);
        }
        res.put("x",realX);
        res.put("y",y);
        return res;
    }
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        Employee employee = this.verify(shop, admin);
        shopMapper.add(shop);
        if (null == employee){
            // 添加emp
            employee = admin;
            employeeMapper.add(employee);
        }
        // 修改emp的shopId
//        Employee employee1 = new Employee();
//        employee1.setId(employee.getId());
//        employee1.setShopId(shop.getId());
        employee.setShopId(shop.getId());
        employeeMapper.update(employee);
        // 修改shop表的adminId
        Shop uptShop = new Shop();
        uptShop.setId(shop.getId());
        uptShop.setAdminId(employee.getId());
        shopMapper.update(uptShop);
    }
    @Override
    public void updeteAgin(Shop shop) {
        Shop shop1 = this.verify(shop);
        shop1.setState(0);
        // 修改emp的shopId
//        Employee employee1 = new Employee();
//        employee1.setId(employee.getId());
//        employee1.setShopId(shop.getId());
        // 修改shop表的adminId
        shopMapper.update(shop1);
    }
    //通过请求
    @Override
    public void auditPass(ShopAuditLog auditLog) {
        // 判断参数
        if (StringUtils.isBlank(auditLog.getNote())){
            throw new BussinessException("备注不能为空！");
        }
        // 修改店铺的状态为 审核通过/待激活
        Shop shop = new Shop();
        shop.setId(auditLog.getShopId());
        shop.setState(1);
        shopMapper.update(shop);
        // 添加店铺日志
        // TODO 我们还没做登陆注册 现在手动写死一个 审核人id
        auditLog.setAuditId(12L);
        auditLogMapper.add(auditLog);
        // 发送邮件
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = sender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1303223697@qq.com");
            helper.setSubject("宠物之家审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>恭喜您审核您审核通过，点击下方链接激活店铺</h1>"+
                    "http://localhost:8081/#/RegisterAgain?id="+auditLog.getShopId(),true);
            // 5.设置收件人
            helper.setTo("1303223697@qq.com");
        } catch (Exception e) {
            throw new BussinessException("发送邮件错误！");
        }
        // 6.发送邮件
        sender.send(mimeMessage);
    }
    //驳回
    @Override
    public void auditre(ShopAuditLog auditLog) {
        // 判断参数
        if (StringUtils.isBlank(auditLog.getNote())){
            throw new BussinessException("备注不能为空！");
        }
        // 修改店铺的状态为 审核通过/待激活
        Shop shop = new Shop();
        shop.setId(auditLog.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);
        // 添加店铺日志
        // TODO 我们还没做登陆注册 现在手动写死一个 审核人id
        auditLog.setAuditId(12L);
        auditLogMapper.add(auditLog);
        // 发送邮件
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = sender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1303223697@qq.com");
            helper.setSubject("宠物之家审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>您的审核未通过，点击下方链接重写填写店铺信息</h1>"+
                    "http://localhost:8081/#/RegisterAgain?id="+auditLog.getShopId(),true);
            // 5.设置收件人
            helper.setTo("1303223697@qq.com");
        } catch (Exception e) {
            throw new BussinessException("发送邮件错误！");
        }
        // 6.发送邮件
        sender.send(mimeMessage);
    }
//    @Override
//    public void updeteAgin(ShopAuditLog auditLog) {
//        // 判断参数
//        if (StringUtils.isBlank(auditLog.getNote())){
//            throw new BussinessException("备注不能为空！");
//        }
//        // 修改店铺的状态为 审核通过/待激活
//        Shop shop = new Shop();
//        shop.setId(auditLog.getShopId());
//        shop.setState(-1);
//        shopMapper.update(shop);
//        // 添加店铺日志
//        // TODO 我们还没做登陆注册 现在手动写死一个 审核人id
//        auditLog.setAuditId(123L);
//        auditLogMapper.add(auditLog);
//        // 发送邮件
//        // 1.创建复杂邮件对象
//        MimeMessage mimeMessage = sender.createMimeMessage();
//        // 2.发送复杂邮件的工具类
//        MimeMessageHelper helper = null;
//        try {
//            helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
//            helper.setFrom("1303223697@qq.com");
//            helper.setSubject("宠物之家审核结果通知");
//            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
//            helper.setText("<h1>您的审核未通过，点击下方链接重写填写店铺信息</h1>"+
//                    "http://localhost:8081/#/RegisterAgain?id="+auditLog.getShopId(),true);
//            // 5.设置收件人
//            helper.setTo("1303223697@qq.com");
//        } catch (Exception e) {
//            throw new BussinessException("发送邮件错误！");
//        }
//        // 6.发送邮件
//        sender.send(mimeMessage);
//    }
    @Override
    public void active(Long id) {
        Shop shop = new Shop();
        shop.setId(id);
        shop.setState(2);
        shopMapper.update(shop);
    }
    private Shop verify(Shop shop) {
        if (StringUtils.isBlank(shop.getName())){
            throw new BussinessException("店铺名字不能为空");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称不合规！");
        }
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺logo不合规！");
        }
        // TODO AI验证
        // 店铺名称是否存在
        return shop;
    }
    private Employee verify(Shop shop, Employee admin) {
        if (StringUtils.isBlank(shop.getName())){
            throw new BussinessException("店铺名字不能为空");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BussinessException("店铺名称不合规！");
        }
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            throw new BussinessException("店铺logo不合规！");
        }
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BussinessException("两次密码不一致！");
        }
        // TODO AI验证
        // 店铺名称是否存在
        Shop tmpShop = shopMapper.findByName(shop.getName());
        if (null != tmpShop){
            throw new BussinessException("店铺已经存在！");
        }
        // 当前管理员是否管理了其他店铺
        Employee employee = employeeMapper.findByName(admin.getUsername());
        if (null != employee && null != employee.getShopId()){
            Shop shopById = shopMapper.findById(employee.getShopId());
            if (shopById.getAdminId().equals(employee.getId())){
                throw new BussinessException("当前管理员已经关联了店铺了！");
            }
        }
        return employee;
    }
}
